﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR;
using Youz.CreditRepay.BLL;

namespace Youz.CreditRepay.SignalR
{
    public enum ClientGroup
    { 
        Default, DataCenter
    }

    public class Ticker
    {
        // Singleton instance
        private readonly static Lazy<Ticker> _instance = new Lazy<Ticker>(
            () => new Ticker(GlobalHost.ConnectionManager.GetHubContext<TickerHub>().Clients));

        public readonly static ConnectionMapping<string> Connections = new ConnectionMapping<string>();
        
        private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(250);

        private Timer _timer;

        private Ticker(IHubConnectionContext<dynamic> clients)
        {
            Clients = clients;

            //_timer = new Timer(new TimerCallback(PushData), this, 5000, 5000);

        }

        public static Ticker Instance
        {
            get
            {
                return _instance.Value;
            }
        }

        private IHubConnectionContext<dynamic> Clients
        {
            get;
            set;
        }

        /// <summary>
        /// 推送行情
        /// </summary>
        /// <param name="data"></param>
        public void PushMarket(dynamic data)
        {
            Clients.Group(ClientGroup.Default.ToString()).pushMarket(data);
        }

        public void PushAccount(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).pushAccount(data);
            }
        }

        public void PushOrder(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).pushOrder(data);
            }
        }

        public void PushContract(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).pushContract(data);
            }
        }

        public void PushContractSummary(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).pushContractSummary(data);
            }
        }

        public void PushMessage(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).pushMessage(data);
            }
        }

        public void LogOut(string memberAccount, dynamic data)
        {
            foreach (var connectionId in Connections.GetConnections(memberAccount))
            {
                Clients.Client(connectionId).logOut(data);
            }
        }
        /// <summary>
        /// 重启刷新客户端
        /// </summary>
        public void Restart()
        {
            Clients.All.restart();
        }
    }
}